<?php
    //打开  
    //$pdo=myPDO::getInstance();
    /*************数据查询***************************/
    //$sql='select uid from tb_admin';
    //$rs=$pdo->query($sql);
    //$data=$rs->fetchAll();//取出所有结果
    //print_r($data);
    /*************数据更新***************************/
    /**$sql='UPDATE t1 SET t1.`id1`=11 WHERE t1.`id1`=1';
    $rs=$pdo->query($sql);*/
    /**
     * 数据库pdo连接
     */
    class myPDO{
        private static $dbConf=array(
            'host'=>'127.0.0.1',
            'user'=>'sa',
            'password'=>'sqlbate',
            'dbName'=>'db_dormitory_management_system',
            'charSet'=>'utf8',
            'port'=>'3306'
        );
        private static $pdo;

        private function __construct(){
            //code
        }
        private function __clone(){
            //code
        }
        /**
         * 获取实例化的PDO，单例模式
         * @return PDO
         */
        public static function getInstance(){
            if(!(self::$pdo instanceof PDO)){
                $dsn ="mysql:host=".self::$dbConf['host'].";port=".self::$dbConf['port'].";dbname=".self::$dbConf['dbName'].";charset=".self::$dbConf['charSet'];
                try {
                    self::$pdo = new PDO($dsn,self::$dbConf['user'], self::$dbConf['password'],
                    array(PDO::ATTR_PERSISTENT => true,PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); //保持长连接
                    self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
                    self::$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);//强制使用MYSQL进行本地预处理，而不是模拟预处理
                } catch (PDOException $e) {
                    print "Error:".$e->getMessage()."<br/>";
                    die();
                }
            }
            return self::$pdo;
        }
    }
?>